﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_FieldDuties : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IClassBLL classBLL = new ClassBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IFieldDutyBLL dutyBLL = new FieldDutyBLL();
        private IFieldDutyDtlsBLL dutyDtlsBll = new FieldDutyDtlsBLL();
        private PrimaryKeyBLL pkBLL = new PrimaryKeyBLL();

        private PrimaryKeyInfo pkInfo;        

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataGrid();
            LoadHeader();          
        }

        public void LoadDataGrid()
        {
            Nullable<int> FieldSeq = Session["FieldSeq"].ToString().Length > 0 ? Int32.Parse(Session["FieldSeq"].ToString()) : (int?)null;
            Nullable<int> ClassSeq = Session["ClassSeq"].ToString().Length > 0 ? Int32.Parse(Session["ClassSeq"].ToString()) : (int?)null;
            pkInfo = pkBLL.GetFieldDutyPK(FieldSeq, ClassSeq);
            //IList<FieldDutyDtlsInfo> Info = dutyDtlsBll.ListFieldDutyDetails((int) pkInfo.Seq);
            DataSet ds = dutyDtlsBll.LoadFieldDutyDetails((int?)null, pkInfo.Seq);
            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["field_duty_dtls_seq"].ToString();

                   dutyDtlsBll.Delete(Int32.Parse(seq), ssMng.EmpSeqI);
            }
            LoadDataGrid();
        }
        public void LoadHeader()
        {
            pkInfo = pkBLL.GetFieldDutyPK(Int32.Parse(Session["FieldSeq"].ToString()), Int32.Parse(Session["ClassSeq"].ToString()));
            IList<FieldDutyInfo> fdutyList = dutyBLL.ListFieldDutiesBySequence((int)pkInfo.Seq);
            if (fdutyList.Count > 0)
            {
                lblSeq.Text = fdutyList[0].Sequence.ToString();
                lblDescr.Text = fdutyList[0].Description;
                txtDescr.Text = lblDescr.Text;
            }
            txtDescr.Visible = false;
            lblDescr.Visible = !txtDescr.Visible;
            btnEdit.Visible = true;
            btnAddNew.Visible = false;
            btnUpdate.Visible = false;
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            lblDescr.Visible = false;
            txtDescr.Visible = true;
            btnAddNew.Visible = lblSeq.Text.Trim().Length <= 0;
            btnUpdate.Visible = !btnAddNew.Visible;
            btnEdit.Visible = false;
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            dutyBLL.Insert(Session["FieldSeq"].ToString()
                , Session["ClassSeq"].ToString()
                , txtDescr.Text
                , "", 1);
            LoadHeader();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            dutyBLL.Update(Int32.Parse(lblSeq.Text.Trim())
                 , Session["FieldSeq"].ToString()
                 , Session["ClassSeq"].ToString()
                 , txtDescr.Text
                 , "", 1);
            LoadHeader();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}